// 全局状态管理
import {createStore} from 'vuex'
import globalClass from "@/config/methods/globalClass.js";

export default createStore({
    /**
     * 集中存储组件的状态
     */
    state: {
        //全局提示信息
        info: {
            msg: "", //提示内容
            type: "", //提示类型
            statue:false,//状态
        }, //全局信息
        headerLoad:"",//头部加载动画
        headerLoadTimer:null, //头部动画定时器
        downHeaderLoadTimer:null,//关闭头部动画定时器
        user:{
            username:" ",
            auto:" ",
            email:" ",
            id:" ",
        }, //用户信息状态
        userWindow:false,//用户信息窗口是否打开
        open:false, // 子评论框控制
        commentsSize:0,//评论个数
        unread_messages:false,//查看收到的评论框，
        admin:false,//是否进入admin模式
    },
    /**
     * 改变数据方法的集合
     */
    mutations: {
        //是否打开收到的评论框
        unread_messages(state,item){
            state.unread_messages = item;
        },
        //控制用户信息窗口打开
        userWindow(state,item){
            state.userWindow = item
        },
        //打开头部加载动画
        headerLoad(state){
            clearTimeout(state.headerLoadTimer)
            state.headerLoad = false;
            setTimeout(()=>{
                state.headerLoad = true;
            },50)
            state.headerLoadTimer = setTimeout(()=>{
                state.headerLoad = true;
            },1000)
        },
        //关闭头部加载动画
        downHeaderLoad(state){
            clearTimeout(state.downHeaderLoadTimer)
            state.downHeaderLoadTimer = setTimeout(()=>{
                state.headerLoad = false;
            },1200)
        },
        //存储用户信息
        user(state,info){
            if(!info){
                state.user.email =null;
                state.user.auto =null;
                state.user.username =null;
                state.user.id =null;
            }
            state.user = info;
        },
        //主题切换
        globalStyles(state, topic) {
            switch (topic) {
                case "daytime": //白天
                    globalClass.daytime();
                    break;
                case "night": //夜晚
                    globalClass.night();
                    break;
            }
        },
        //全局弹窗信息
        info_sub(state, message) {
            if(state.info.statue){
                return;
            }
            state.info.statue = true;
            state.info.msg = message.msg;
            state.info.type = message.type;
            setTimeout(() => {
                state.info.msg = "";
                state.info.statue = false;
            }, 2000);
        },
        //评论个数增减
        commentsSize(state,size){
           state.commentsSize = size;
        }
    },
    /**
     * 改变mutations，不直接变更状态
     */
    actions: {}
    ,
    /**
     * 可以将store进行功能拆分，分割成不同的模块
     */
    modules: {}


})